package com.example.mydy;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.switchmaterial.SwitchMaterial;

public class SettingsActivity extends AppCompatActivity {
    private static final String TAG = "SettingsActivity";
    private static final String PREFS_NAME = "MyAppPrefs";
    
    private SwitchMaterial switchNotification;
    private SwitchMaterial switchDarkMode;
    private TextView tvCacheSize;
    private TextView tvVersion;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        
        // 初始化Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setTitle("设置");
            getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        }
        
        // 初始化视图
        initViews();
        
        // 加载设置
        loadSettings();
        
        // 设置底部导航栏
        setupBottomNavigation();
    }

    private void initViews() {
        switchNotification = findViewById(R.id.switch_notification);
        switchDarkMode = findViewById(R.id.switch_dark_mode);
        tvCacheSize = findViewById(R.id.tv_cache_size);
        tvVersion = findViewById(R.id.tv_version);
        
        // 设置版本信息
        try {
            String versionName = getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
            tvVersion.setText("版本 " + versionName);
        } catch (Exception e) {
            tvVersion.setText("版本 1.0.0");
        }
        
        // 设置缓存大小
        tvCacheSize.setText("0.00 MB");
        
        // 设置开关监听器
        switchNotification.setOnCheckedChangeListener((buttonView, isChecked) -> {
            SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
            editor.putBoolean("notifications_enabled", isChecked);
            editor.apply();
            
            if (isChecked) {
                Toast.makeText(this, "通知已开启", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "通知已关闭", Toast.LENGTH_SHORT).show();
            }
        });
        
        switchDarkMode.setOnCheckedChangeListener((buttonView, isChecked) -> {
            SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
            editor.putBoolean("dark_mode_enabled", isChecked);
            editor.apply();
            
            Toast.makeText(this, "需要重启应用以应用主题更改", Toast.LENGTH_SHORT).show();
        });
        
        // 设置清除缓存点击事件
        findViewById(R.id.layout_clear_cache).setOnClickListener(v -> {
            // 模拟清除缓存
            Toast.makeText(this, "缓存已清除", Toast.LENGTH_SHORT).show();
            tvCacheSize.setText("0.00 MB");
        });
        
        // 设置关于我们点击事件
        findViewById(R.id.layout_about).setOnClickListener(v -> {
            Toast.makeText(this, "关于我们", Toast.LENGTH_SHORT).show();
            // 这里可以启动"关于我们"页面
        });
        
        // 设置隐私政策点击事件
        findViewById(R.id.layout_privacy).setOnClickListener(v -> {
            Toast.makeText(this, "隐私政策", Toast.LENGTH_SHORT).show();
            // 这里可以启动"隐私政策"页面
        });
        
        // 设置退出登录点击事件
        findViewById(R.id.btn_logout).setOnClickListener(v -> {
            logout();
        });
    }
    
    private void loadSettings() {
        SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        boolean notificationsEnabled = prefs.getBoolean("notifications_enabled", true);
        boolean darkModeEnabled = prefs.getBoolean("dark_mode_enabled", false);
        
        switchNotification.setChecked(notificationsEnabled);
        switchDarkMode.setChecked(darkModeEnabled);
    }
    
    private void setupBottomNavigation() {
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        
        // 设置当前选中项为"设置"
        bottomNavigationView.setSelectedItemId(R.id.nav_settings);
        
        // 设置导航菜单项选择监听器
        bottomNavigationView.setOnItemSelectedListener(item -> {
            int itemId = item.getItemId();
            if (itemId == R.id.nav_home) {
                // 设置选中项为首页，然后跳转
                bottomNavigationView.setSelectedItemId(R.id.nav_home);
                startActivity(new Intent(this, MainActivity.class));
                finish();
                return true;
            } else if (itemId == R.id.nav_advertisement) {
                // 设置选中项为广告，然后跳转
                bottomNavigationView.setSelectedItemId(R.id.nav_advertisement);
                startActivity(new Intent(this, AdvertisementListActivity.class));
                finish();
                return true;
            } else if (itemId == R.id.nav_project) {
                // 设置选中项为项目，然后跳转
                bottomNavigationView.setSelectedItemId(R.id.nav_project);
                startActivity(new Intent(this, ProjectActivity.class));
                finish();
                return true;
            } else if (itemId == R.id.nav_settings) {
                // 当前页面，无需操作
                return true;
            }
            return false;
        });
    }
    
    private void logout() {
        // 清除用户数据和登录状态
        SharedPreferences.Editor editor = getSharedPreferences(PREFS_NAME, MODE_PRIVATE).edit();
        editor.clear();
        editor.apply();
        
        Toast.makeText(this, "已退出登录", Toast.LENGTH_SHORT).show();
        
        // 跳转到登录页面
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }
} 